/**
 * 游戏 场景 基类
 */
class GameBaseScene
    extends egret.Sprite {

    /**
     * 编译顺序 标识
     */
    public static setReferenceFlag: string = PhysicsSpace.referenceFlag;

    /**
     * 相关属性
     */
    public initCompleteFlag: boolean = false; // 是否 已完成 初始化 标识
    public selectMenuOption: string = ""; // 当前 选中 菜单 选项
    public menuOptionList: Array<MenuOptionSprite> = []; // 菜单 选项 集合


    /**
     * 重置 场景 宽高
     */
    public resetSceneSize(): void {
        this.width = GameHelper.getInstance.stageWidth; // 设置 宽高
        this.height = GameHelper.getInstance.stageHeight;
    }

    /**
     * 绘制 图形
     */
    public debugShapeDraw(): void {

        let setWidth: number = this.width;
        let setHeight: number = this.height;

        this.graphics.beginFill(0x4876ff, 0.5);
        this.graphics.drawRect(0, 0, setWidth, setHeight);
        this.graphics.endFill();
    }

    /**
     * 设置 初始化 标识
     * @param setFlag
     */
    public setInitCompleteFlag(setFlag: boolean): void {

        let setVal: boolean = setFlag;
        this.initCompleteFlag = setVal;
    }

    /**
     * 根据 选中 选项，进入 游戏 场景
     */
    public enterSceneByOption(): void {

        let menuOption: string = this.selectMenuOption;

        switch (menuOption) {

            case CommonData.MAIN_MENU_OPTION:
                EventDispatcherManager.getInstance.returnGameStartSceneNotify();
                break;
            case CommonData.PAUSE_MENU_OPTION:
                // this.changeOptionStatus(false);
                EventDispatcherManager.getInstance.returnGameFireSceneNotify();
                break;
            case CommonData.START_MENU_OPTION:
                EventDispatcherManager.getInstance.enterGameFireSceneNotify();
                break;
            case CommonData.TEST_MENU_OPTION:
                // this.changeOptionStatus(false);
                break;
            default:
                // console.log("default");
                break;
        }
    }

    /**
     * 键盘管理器 心跳机制 执行函数
     * @param sceneName
     */
    public handleKeyDown(sceneName: string): void {

        let keyBoardStatus: boolean = GameHelper.getInstance.getKeyBoardStatus();
        // console.log("Pause Status：" + keyBoardStatus);

        if (keyBoardStatus == false) { // 假如 键盘对象 处于 休眠状态，跳过 此次 键盘事件
            return;
        }

        let getScene: string = sceneName;
        let inputsLength: number = KeyBoardManager.getInstance.keyBoard.inputs.length;

        if (inputsLength == 1) {

            let inputStr: any = KeyBoardManager.getInstance.keyBoard.inputs[0];

            switch (inputStr) {

                case KeyBoard.UpArrow:
                    this.changeOptionStatus(true);
                    break;
                case KeyBoard.DownArrow:
                    this.changeOptionStatus(false);
                    break;
                case KeyBoard.CnterEnter:
                    this.enterSceneByOption();
                    GameHelper.getInstance.keyBoardDelay(); // 避免 场景 键盘事件 冲突，发起 键盘事件 延迟 操作
                    break;
                case KeyBoard.Esc:
                    this.keyDownByEse(getScene);
                    break;
                default:
                    // console.log("default");
                    break;
            }

        }
    }

    /**
     * 根据 场景 处理 Ese 按键 事件
     * @param sceneName
     */
    public keyDownByEse(sceneName: string): void {

        let getScene: string = sceneName;
        let resultOne: boolean = getScene == CommonData.GAME_START_SCENE_ID; // 开始 场景
        let resultTwo: boolean = getScene == CommonData.GAME_PAUSE_SCENE_ID; // 暂停 场景

        if (resultOne) {

        } else if (resultTwo) {
            EventDispatcherManager.getInstance.returnGameFireSceneNotify();
            GameHelper.getInstance.keyBoardDelay(); // 避免 场景 键盘事件 冲突，发起 键盘事件 延迟 操作
        }

    }


    /**
     * 变换 菜单 选项 状态
     * @param arrow
     */
    public changeOptionStatus(arrow: boolean): void {

        // 1.0 获取 选择方向（上下）
        let getFlag: boolean = arrow; // 向上：true，向下：false
        let listMax: number = this.menuOptionList.length - 1;
        let currentMenuOption: string = this.selectMenuOption;

        // 2.0 获取 当前 选中项
        let getCurrentNum: number = 0;

        for (let i = 0; i < this.menuOptionList.length; i++) {
            if (this.menuOptionList[i].menuOptionName == currentMenuOption) {
                getCurrentNum = i;
            }
        }

        // 3.0 获取 接下来 选中项
        let isMax: boolean = getCurrentNum == listMax;
        let isFirst: boolean = getCurrentNum == 0;
        let isDown: boolean = getFlag == false;
        let isUp: boolean = getFlag == true;

        if (isMax && isDown) { // 假如 已经是 最底端 选项，并且是 向下操作
            return;
        } else if (isUp && isFirst) {
            return;
        }

        if (isDown == true) {
            let newNum: number = getCurrentNum + 1;
            let getOptionName: string = this.menuOptionList[newNum].menuOptionName;
            this.setActiveMenuOption(getOptionName);
        } else if (isUp == true) {
            let newNum: number = getCurrentNum - 1;
            let getOptionName: string = this.menuOptionList[newNum].menuOptionName;
            this.setActiveMenuOption(getOptionName);
        }
    }


    /**
     * 设置 当前 选中 菜单 选项
     * @param menuOption
     */
    public setActiveMenuOption(menuOption: string): void {

        let getMenuOption: string = menuOption;
        this.selectMenuOption = getMenuOption;

        for (let i = 0; i < this.menuOptionList.length; i++) {

            this.menuOptionList[i].setOptionStatus(false); // 所有 选项，改为 未选状态

            if (this.menuOptionList[i].menuOptionName == getMenuOption) {
                this.menuOptionList[i].setOptionStatus(true); // 选中项 改为 选中状态
            }
        }
    }


}